<#macro getWordF tableAlias>
    <#list tableAlias?split("_") as word>
        ${word? substring(0,1) }<#t/>
    </#list>
</#macro>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.dao.rdb.${moduleName}.${table.className}DAO">
    <resultMap id="${table.className}ResultMap" type="${basePackage}.dao.rdb.${moduleName}.${table.className}DO">

    <#list table.primaryKeys as key>
        <id column="${key.columnName}" property="${key.javaProperty}" jdbcType="${key.jdbcTypeName}"/>
    </#list>

    <#list table.baseColumns as column>
        <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
        <result column="${column.columnName}" property="${column.javaProperty}" jdbcType="${column.jdbcTypeName}"/>
        </#if>
    </#list>

    </resultMap>


    <sql id="Base_${table.className}_Column_List">
        <trim suffixOverrides=",">
        <#list table.columns as column>
            <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
                <@getWordF tableAlias="${table.tableAlias}" />.${column.columnName},<#t/>
            </#if>
        </#list>
        </trim>
    </sql>

    <sql id="Base_${table.className}_Key_List">
        <trim suffixOverrides=",">
        <#list table.primaryKeys as key>
            <@getWordF tableAlias="${table.tableAlias}" />.${key.columnName},<#t/>
        </#list>
        </trim>
    </sql>

    <sql id="Base_${table.className}_Table">
        ${table.tableName} <@getWordF tableAlias="${table.tableAlias}" />
    </sql>

    <!-- 有效数据状态 -->
    <sql id="Base_DataStatus_Query">
        data_status = 0
    </sql>

    <sql id="Base_DataStatus_Query_${table.className}_Alias">
        <@getWordF tableAlias="${table.tableAlias}" />.data_status = 0
    </sql>

    <!-- 全插入 -->
    <insert id="insert${table.className}" parameterType="${basePackage}.dao.rdb.${moduleName}.${table.className}DO">
        insert into ${table.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.columns as column>
                 <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
                     ${column.columnName},<#t/>
                 </#if>
            </#list>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
         <#list table.columns as column>
             <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
                 ${r'#{'}${column.javaProperty},jdbcType=${column.jdbcTypeName}${r'}'},<#t/>
             </#if>
         </#list>
        </trim>
    </insert>

    <!-- 部分插入 只更新不为null的字段 -->
    <insert id="insert${table.className}Selective" parameterType="${basePackage}.dao.rdb.${moduleName}.${table.className}DO">
        insert into ${table.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list table.columns as column>
            <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
            <if test="${column.javaProperty} != null">
                ${column.columnName},
            </if>
            </#if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list table.columns as column>
        <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
            <if test="${column.javaProperty} != null">
                ${r'#{'}${column.javaProperty},jdbcType=${column.jdbcTypeName}${r'}'},
            </if>
        </#if>
        </#list>
        </trim>
    </insert>

    <!-- 通过主键查询 dataStatus自动引入 -->
    <select id="select${table.className}ByPrimaryKey" resultMap="${table.className}ResultMap">
        select
        <include refid="Base_${table.className}_Column_List"/>
        from <include refid="Base_${table.className}_Table"/>
        <trim prefix="WHERE">
            <include refid="Base_DataStatus_Query_${table.className}_Alias" />
            <#list table.primaryKeys as key>
                AND ${key.columnName} = ${r'#{'}${key.javaProperty},jdbcType=${key.jdbcTypeName}${r'}'}
            </#list>
        </trim>
    </select>

    <!-- 通过DO查询的条件拼装 -->
    <sql id="doquery">
        <trim prefix="WHERE" prefixOverrides="AND">
            <include refid="Base_DataStatus_Query"/>
            <#list table.columns as column>
                <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
                    <if test="${column.javaProperty} != null <#if column.isString()> and ${column.javaProperty} != ''</#if>">
                        AND <@getWordF tableAlias="${table.tableAlias}" />.${column.columnName} = ${r'#{'}${column.javaProperty}${r'}'}
                    </if>
                </#if>
            </#list>
        </trim>
    </sql>

    <!-- 通过DO查询 -->
    <select id="selectBy${table.className}" parameterType="${basePackage}.dao.rdb.${moduleName}.${table.className}DO"
            resultMap="${table.className}ResultMap">
        SELECT
        <include refid="Base_${table.className}_Column_List"/>
        FROM <include refid="Base_${table.className}_Table"/>
        <include refid="doquery" />
    </select>

    <!-- 通过DO查询数量 -->
    <select id="select${table.className}Count" parameterType="${basePackage}.dao.rdb.${moduleName}.${table.className}DO"
            resultType="java.lang.Integer">
        SELECT count( <include refid="Base_${table.className}_Key_List"/> )
        FROM <include refid="Base_${table.className}_Table"/>
        <include refid="doquery" />
    </select>

    <!-- 通过主键更新记录 全更新-->
    <update id="update${table.className}ByPrimaryKey" parameterType="${basePackage}.dao.rdb.${moduleName}.${table.className}DO">
        update ${table.tableName}
        <trim prefix="SET" suffixOverrides=",">
            <#list table.baseColumns as column>
                <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
                    ${column.columnName} =  ${r'#{'}${column.javaProperty},jdbcType=${column.jdbcTypeName}${r'}'},
                </#if>
            </#list>
        </trim>
        <trim prefix="WHERE" prefixOverrides="AND">
            <#list table.primaryKeys as key>
                AND ${key.columnName} = ${r'#{'}${key.javaProperty},jdbcType=${key.jdbcTypeName}${r'}'}
            </#list>
        </trim>
    </update>

    <!-- 通过主键更新记录 部分更新-->
    <update id="update${table.className}ByPrimaryKeySelective" parameterType="${basePackage}.dao.rdb.${moduleName}.${table.className}DO">
        update ${table.tableName}
        <trim prefix="SET" suffixOverrides=",">
            <#list table.baseColumns as column>
                <#if column.columnName!='data_status' && column.columnName != 'create_time'&& column.columnName != 'update_time'>
                    <if test="${column.javaProperty} != null">
                        ${column.columnName} =  ${r'#{'}${column.javaProperty},jdbcType=${column.jdbcTypeName}${r'}'},
                    </if>
                </#if>
            </#list>
        </trim>
        <trim prefix="WHERE" prefixOverrides="AND">
            <#list table.primaryKeys as key>
                AND ${key.columnName} = ${r'#{'}${key.javaProperty},jdbcType=${key.jdbcTypeName}${r'}'}
            </#list>
        </trim>
    </update>

    <!-- 删除数据 其实是更新状态 -->
    <update id="del${table.className}ByPrimaryKey">
        update ${table.tableName}
        set data_status = 1
        <trim prefix="WHERE" prefixOverrides="AND">
            <#list table.primaryKeys as key>
                AND ${key.columnName} = ${r'#{'}${key.javaProperty},jdbcType=${key.jdbcTypeName}${r'}'}
            </#list>
        </trim>
    </update>


</mapper>
